//
//  OrderDetail.m
//  Wujin
//
//  Created by 东 on 16/12/10.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "OrderDetail.h"

#import "OrderDescriptionCell.h"
#import "UserInfoCell.h"

@interface OrderDetail ()
{
    CGFloat _totalPrice;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation OrderDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    
    for (OrderGoodsModel *model in _dataArray)
    {
        _totalPrice += [model.TotalPrice floatValue];
    }
    
    [_tableView reloadData];
}

#pragma mark tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.dataArray.count;
        
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 121;
    }
    else
    {
        return 265;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        OrderDescriptionCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"goodscell"];
        
        OrderGoodsModel *model = _dataArray[indexPath.row];
        
        cell.model = model;
        cell.index = indexPath.row;
        return cell;
    }
    else
    {
        UserInfoCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"userInfo"];
        cell.totalPrice = _totalPrice;
        cell.dataArray = _dataArray;
        __weak OrderDetail *weakSelf = self;
        cell.block = ^(){
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return 26;
    else
        return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    if (!view)
    {
        view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
    }
    
    
    UILabel *lab = [view viewWithTag:101];
    if (!lab)
    {
        lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 240, 26)];
        [view addSubview:lab];
        lab.tag = 101;
        lab.font = [UIFont systemFontOfSize:17 weight:UIFontWeightLight];
        lab.textColor = skin.skinColor;
    }
    view.backgroundColor = [UIColor whiteColor];
    lab.text = @"商品描述";
    lab.font = [UIFont systemFontOfSize:16];
    
    return view;
}

//当tableview 滚动的时候 结束编辑事件  （退出键盘）
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - 处理键盘事件
- (void)keyboardDidChangeFrame:(NSNotification *)noti
{
    self.view.window.backgroundColor = _tableView.backgroundColor;
    
    CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat keyY = frame.origin.y;
    
    CGFloat screenH = [[UIScreen mainScreen] bounds].size.height;
    
    CGFloat keyDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    _topCons.constant = (keyY - screenH);
    [UIView animateWithDuration:keyDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}
@end
